/**
 *	个人管理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-29
 */
package com.framework.Controller.api;


import com.framerock.db.DBWhere;
import com.framerock.server.Response;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Patter;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockMsg;
import com.framework.Chajian.ChajianSms;
import com.framework.ACore.Model;
import com.framework.Model.ModelLogin;
import com.framework.Model.ModelUsera;
import com.framework.Model.ModelUsers;

import java.util.Map;


public class ControllerGeren extends ControllerApi {



    //执行方法
    protected String runAction(String action)
    {
        if(action.equals("exit"))return exit();
        if(action.equals("getcode"))return getcode();
        if(action.equals("editpass"))return editpass();
        if(action.equals("editface"))return editface();
        if(action.equals("editmobile"))return editmobile();
        return super.runAction(action);
    }


    /**
     * 退出
     * */
    public String exit()
    {
        new ModelLogin(this.nDb).exit(this.admintoken);
        return Response.returnSuccess("exitok");
    }

    /**
     * 修改密码
     * */
    public String editpass()
    {
        String opass = mRequest.post("opass");
        String pass  = mRequest.post("pass");
        if(Rock.isEmpt(opass) || Rock.isEmpt(pass)) {
        }else {
            Model obj   = ModelUsers.get(this.nDb);
            Map<String, String> rs = obj.getOne(mRequest.USERID);
            String ostr = rs.get("pass");
            if(!ostr.equals(Jiami.md5(opass+rs.get("rand"))))
                return Response.returnError("旧密码错误");

            String rand = CDate.gettime();
            obj.update(Rock.getMap(new String[]{
                    "rand",rand,
                    "pass",Jiami.md5(pass+rand)
            }),mRequest.USERID);
        }
        return Response.returnSuccess("ok");
    }

    /**
     * 修改头像
     * */
    public String editface()
    {
        String face = mRequest.get("face");
        if(!Rock.isEmpt(face)){
            face = Jiami.base64decode(face);
            ModelUsers.get(this.nDb).update("`face`='"+face+"'", mRequest.USERID);
        }
        return Response.returnSuccess("ok");
    }

    public String editmobile()
    {
        String mobile = mRequest.get("mobile");
        String yzm    = mRequest.get("yzm");
        if(Rock.isEmpt(mobile) || Rock.isEmpt(yzm))return Response.returnError("params err");

        RockMsg rockmsg = ChajianSms.obj(this.nDb).CheckCode(mobile, "edit", yzm);
        if(rockmsg.isError())return rockmsg.back();

        Model uobj = ModelUsers.get(this.nDb);
        int to = uobj.getCount("`mobile`='"+mobile+"'");
        if(to>0)return Response.returnError("手机号已使用");
        uobj.update("`mobile`='"+mobile+"'", mRequest.USERID);
        ModelUsera.get(this.nDb).update("`mobile`='"+mobile+"'", "`uid`="+mRequest.USERID+"");
        return Response.returnSuccess("ok");
    }

    //发送验证码
    private String getcode()
    {
        String mobile   = mRequest.get("mobile");
        if(Rock.isEmpt(mobile) || !Patter.isnumber(mobile))return Response.returnError("errcan");

        int to = ModelUsers.get(this.nDb).getCount(DBWhere.start().wstro("mobile", mobile).show());
        if(to>0)return Response.returnError("手机号已使用");

        return ChajianSms.obj(this.nDb).sendCode(mobile, "edit");
    }
}
